package com.example.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.education.Bean.ForeignSubjects;
import com.example.education.Bean.StudentInfo;
import com.example.education.Mapper.ForeignSubjectsMapper;
import com.example.education.service.ForeignSubjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚
 * @since 2023-03-27
 */
@Service
@Transactional
public class ForeignSubjectsServiceImpl extends ServiceImpl<ForeignSubjectsMapper, ForeignSubjects> implements ForeignSubjectsService {


    @Autowired
    private ForeignSubjectsMapper foreignSubjectsMapper;

    //改
    @Override
    public int insert(ForeignSubjects foreignSubjects) {

        if (foreignSubjects.getUid() == null) {
            return foreignSubjectsMapper.insert(foreignSubjects);
        } else {
            UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", foreignSubjects.getUid());
            updateWrapper.set("grade", foreignSubjects.getSubjectName());
            return foreignSubjectsMapper.update(foreignSubjects, updateWrapper);
        }
    }

    //根据传来的学校名字查询所有课程
    @Override
    public ForeignSubjects[] select(String universityName) {
        return foreignSubjectsMapper.select(universityName);
    }


    @Override
    public int update(ForeignSubjects foreignSubjects) {
        if (foreignSubjects.getUid() == null) {
            return foreignSubjectsMapper.insert(foreignSubjects);
        } else {
            UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", foreignSubjects.getUid());
            updateWrapper.set("grade", foreignSubjects.getSubjectName());
            return foreignSubjectsMapper.update(foreignSubjects, updateWrapper);
        }
    }


}
